
package au.com.lastweekend.jim.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;

import org.apache.log4j.Logger;

import au.com.lastweekend.jim.swing.SwingUtil;
import au.com.lastweekend.jim.swing.folderchooser.FolderChooser;
import au.com.lastweekend.jim.swing.folderchooser.FolderChooserController;

public class OpenFolderAction extends AbstractAction {

    private static final Logger LOG = Logger.getLogger(OpenFolderAction.class);
    private static final long serialVersionUID = 1L;

    public interface OpenFolderController extends FolderChooserController {

        void openFolder(File chosenDir);

    }

    private OpenFolderController controller;

    public OpenFolderAction(OpenFolderController controller, boolean forToolbar) {

        super("Open");
        this.controller = controller;
        if (forToolbar) {
            putValue(SwingUtil.TOOLBAR, Boolean.TRUE);
        } else {
            putValue(SwingUtil.MENU, "File");
        }

        SwingUtil.setActionParameters(this, "folder_go.png", "Open folder", "Go to selected folder");

    }

    public void actionPerformed(ActionEvent e) {

        File chosenDir = getFolderChooser().chooseFolder(controller.getCurrentDirectory());
        if (chosenDir != null) {
            if (chosenDir.isDirectory()) {

                controller.openFolder(chosenDir);
            } else {
                LOG.warn(chosenDir.getPath() + " is not a folder");
            }
        }
    }

    private FolderChooser getFolderChooser() {

        return getValue(SwingUtil.TOOLBAR) != null ? controller.getToolBarFolderChooser() : controller.getDialogFolderChooser();
    }
}